<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.Project"%>
<%@page import="db.TobDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Purreq" %>
<%@page import="db.Supplier" %>
<%@page import="db.Place" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.BorreqDAO"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Outpurmat"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="db.Outreq"%>
<%@page import="db.OutreqDAO"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.Borreq"%>
<%@page import="db.Bormat"%>
<%@page import="db.BormatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	OutpurmatDAO outpurmatDAO = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	ProjectDAO projectDAO = (ProjectDAO)ctx.getBean("ProjectDAO");
	TeamDAO teamDAO = (TeamDAO)ctx.getBean("TeamDAO");
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	PurmatDAO purmatDAO = (PurmatDAO)ctx.getBean("PurmatDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	BorreqDAO borreqDAO = (BorreqDAO)ctx.getBean("BorreqDAO");
	BormatDAO bormatDAO = (BormatDAO)ctx.getBean("BormatDAO");
	String pid = request.getParameter("pid");
	
	Borreq borreq = new Borreq();
	Tob tob = new Tob();
	Team team = new Team();
	Project project = new Project();
	Supplier supplier = new Supplier();
	User requser = new User();
	User mauser = new User();
	User assuser = new User();
	Material material = new Material();
	List borreqlist = new ArrayList();
	if(pid!=null){
		borreq = borreqDAO.findById(Integer.parseInt(pid));
		//outreq = outreqDAO.findById(Integer.parseInt(pid));
		//outpurmatlist = outpurmatDAO.findByOutReqIdAndStateDel(Integer.parseInt(pid),1);
		borreqlist = bormatDAO.findByBorreqIdAndState(Integer.parseInt(pid),1);
	}
	String tobName = "";
	
	if(borreq!=null){//查询业务类型
		int tid = 0;
		tid = borreq.getTobId();
		project =  borreq.getProject();
		tob = tobDAO.findById(tid);
		if(tob!=null){
			tobName = tob.getName();
		}else{
			tobName = "";
		}
		
		int teamid = 0;
		teamid = borreq.getTeamId();
		int suppid = 0;
		team = teamDAO.findById(teamid);
		int requid = 0;
		if(borreq.getReqUserId()!=null){
			requid = borreq.getReqUserId();
			requser = userDAO.findById(requid);
		}
		int mauid = 0;
		if(borreq.getMaUserId()!=null){
			mauid = borreq.getMaUserId();
			mauser = userDAO.findById(mauid);
		}
		int assuid = 0;
		if(borreq.getAssUserId()!=null){
			assuid = borreq.getAssUserId();
			assuser = userDAO.findById(assuid);
		}
		
		
	}

	
	request.setAttribute("purmatlist",getListReMap2(borreqlist));
	request.setAttribute("requser",requser);
	request.setAttribute("mauser",mauser);
	request.setAttribute("assuser",assuser);
	request.setAttribute("supplier",supplier);
	request.setAttribute("team",team);
	request.setAttribute("project",project);
	request.setAttribute("tobName",tobName);
	request.setAttribute("borreq",borreq);
	request.setAttribute("pid",pid);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	request.setAttribute("assdate",sdf.format(new Date()));
	pageContext.forward("/vm/borrowStock/borrowApp_2.vm");
%>
<%!

	public List getListReMap2(List list){
		List listmap = new ArrayList();
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = new HashMap();
				Bormat bormat = new Bormat();
				bormat = (Bormat)list.get(i);
				if(bormat!=null){
					map.put("outpid", bormat.getId());//
					map.put("reqnum", bormat.getReqNum());//申购数量
					map.put("manum", bormat.getMaNum());//审批数量
					map.put("taskcode", bormat.getTaskCode());
					//
					//获取总库存表相应数据
					//Receivepurmat receivepurmat = outpurmat.getReceivepurmat();
					
					Receivepurmat receivepurmat =bormat.getReceivepurmat();// receivepurmatDAO.findById(outpurmat.getReceivePurmatId());
					//System.out.println("test=====================>"+outpurmat.getReceivePurmatId());
					if(receivepurmat!=null){
					map.put("purprice",receivepurmat.getPurPrice());//申购价格
					map.put("patch",receivepurmat.getBatch());//
					Place place = receivepurmat.getPlace();
						if(place!=null){
							map.put("place", place.getName());
						}else{
							map.put("place","");
						}
						if(receivepurmat.getMaterial()!=null){
							Material material = receivepurmat.getMaterial();
							map.put("matcode", material.getCode());
							map.put("matname", material.getName());
							map.put("matcom", material.getCompany());
						}
					}
						
					
					
				}
				listmap.add(map);
			}
		}
		return listmap;
	}
%>